//
//  TitieView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/5.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "TitieView.h"
#import "UILabel+BasicAttributes.h"
#import "UIButton+BaseAttributes.h"
#define NormalColor [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f]
#define SelectColor [UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:1.00f]

@interface TitieView()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *titleScroller;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UIButton *operationBtn;
@property (nonatomic,strong)UIButton *vehicelBtn;
@property (nonatomic,strong)UIButton *driveBtn;
@property (nonatomic,strong)UIButton *alarmBtn;
@end

@implementation TitieView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    _titleScroller = [UIScrollView new];
    _titleScroller.delegate = self;
    _titleScroller.showsVerticalScrollIndicator = NO;
    _titleScroller.showsHorizontalScrollIndicator = NO;
    _titleScroller.bounces = NO;
    _titleScroller.contentSize = CGSizeMake(kWidthOfPt(310)*4, 0);
    [self addSubview:_titleScroller];
    [_titleScroller mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self);
    }];
    
    _operationBtn = [UIButton bttonWithTitle:@"运营情况" andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    [_operationBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_titleScroller addSubview:_operationBtn];
    [_operationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleScroller);
        make.width.mas_equalTo(kWidthOfPt(310));
        make.left.mas_equalTo(_titleScroller.mas_left).offset(kWidthOfPt(310)*0);
    }];

    _vehicelBtn = [UIButton bttonWithTitle:@"车辆分布" andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    [_vehicelBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_titleScroller addSubview:_vehicelBtn];
    [_vehicelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleScroller);
        make.width.mas_equalTo(kWidthOfPt(310));
        make.left.mas_equalTo(_titleScroller.mas_left).offset(kWidthOfPt(310)*1);
    }];

    _driveBtn = [UIButton bttonWithTitle:@"行驶情况" andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    [_driveBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];

    [_titleScroller addSubview:_driveBtn];
    [_driveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleScroller);
        make.width.mas_equalTo(kWidthOfPt(310));
        make.left.mas_equalTo(_titleScroller.mas_left).offset(kWidthOfPt(310)*2);
    }];

    _alarmBtn = [UIButton bttonWithTitle:@"报警情况" andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    [_alarmBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_titleScroller addSubview:_alarmBtn];
    [_alarmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleScroller);
        make.width.mas_equalTo(kWidthOfPt(310));
        make.left.mas_equalTo(_titleScroller.mas_left).offset(kWidthOfPt(310)*3);
    }];

    _lineView = [[UIView alloc] initWithFrame:CGRectMake(kWidthOfPt(310)*0, 142/3, kWidthOfPt(310), 3)];
    _lineView.backgroundColor = SelectColor;
    [_titleScroller addSubview:_lineView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}
- (void)btnDidClick:(UIButton *)sender
{
    NSInteger index;
    if ([sender.titleLabel.text isEqualToString:@"运营情况"]) {
        index = 0;
    }else if ([sender.titleLabel.text isEqualToString:@"车辆分布"]){
        index = 1;
    }else if ([sender.titleLabel.text isEqualToString:@"行驶情况"]){
        index = 2;
    }else if ([sender.titleLabel.text isEqualToString:@"报警情况"]){
        index = 3;
    }else{
        index = 0;
    }
    [self changeSelectItemWithIndex:index];
    if (_titleSelectIndex) {
        _titleSelectIndex(index);
    }
}
-(void)changeSelectItemWithIndex:(NSInteger)index
{
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.frame = CGRectMake(kWidthOfPt(310)*index, 142/3, kWidthOfPt(310), kHeightOfPt(9));
    } completion:^(BOOL finished) {
//        CGFloat lineViewX = self.lineView.frame.origin.x;
        CGFloat lineViewW = self.lineView.frame.size.width;
//        _operationBtn.selected = index==0;
//        _vehicelBtn.selected = index==1;
//        _driveBtn.selected = index==2;
//        _alarmBtn.selected = index==3;
        if (index == 3||index == 2) {
            [self.titleScroller setContentOffset:CGPointMake(lineViewW*4-kScreenWidth, 0) animated:YES];
        }else{
            [self.titleScroller setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    }];
    
}
@end
